package com.laopan.elasticsearch.handler;

import com.laopan.elasticsearch.core.operation.IndexOperation;
import org.elasticsearch.common.xcontent.XContentBuilder;

/**
 * 操作索引
 *
 * @author laopan
 * @className AbstractEsIndexHandler
 * @date 2021/4/16 15:28
 */

public abstract class AbstractIndexHandler extends AbstractIndexInfo {
    private IndexOperation esIndexOperation;

    public AbstractIndexHandler(IndexOperation esIndexOperation) {
        this.esIndexOperation = esIndexOperation;
    }

    public IndexOperation getEsIndexOperation() {
        return esIndexOperation;
    }

    public void setEsIndexOperation(IndexOperation esIndexOperation) {
        this.esIndexOperation = esIndexOperation;
    }

    /**
     * 删除索引
     */
    public boolean deleteIndex() {
        return esIndexOperation.deleteIndex(getIndex());
    }

    /**
     * 创建索引
     */
    public boolean createIndex() {
        XContentBuilder xContentBuilder = getIndexContent();
        if (null != xContentBuilder) {
            return esIndexOperation.createIndex(getIndex(), xContentBuilder);
        }
        return false;
    }

    /**
     * 检查索引是否存在
     *
     * @return
     */
    public boolean existIndex() {
        return esIndexOperation.isExist(getIndex());
    }


    /**
     * 获取创建的索引内容
     *
     * @return
     */
    public abstract XContentBuilder getIndexContent();
}
